package com.bestcem.xm.deliver.convert.grpc;

import com.bestcem.bp.xm.user.grpc.v1.services.Smtp;
import com.bestcem.bp.xm.user.grpc.v1.services.User;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.deliver.entity.*;
import com.bestcem.xm.deliver.grpc.client.dto.*;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.member.grpc.v1.common.ExtendFieldValue;
import com.bestcem.xm.member.grpc.v1.common.LabelValue;
import com.bestcem.xm.member.grpc.v1.common.Member;
import com.bestcem.xm.push.grpc.v1.services.MsgTemplate;
import com.bestcem.xm.push.grpc.v1.services.SendSingleResult;
import com.bestcem.xm.push.grpc.v1.services.WechatOfficialAccountInfo;
import com.bestcem.xm.survey.grpc.v1.services.GetRespondentBySeqResponse;
import com.bestcem.xm.survey.grpc.v1.services.MemberNode;
import com.bestcem.xm.survey.grpc.v1.services.Respondent;
import com.bestcem.xm.survey.grpc.v1.services.RespondentExtra;
import com.google.protobuf.util.Timestamps;
import org.mapstruct.*;

import java.time.Instant;
import java.util.List;
import java.util.Objects;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
        collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
        uses = {BaseConvertMapper.class},
        imports = {DeliverAwardUseType.class, DeliverDelayFailedTtype.class, DeliverScenes.class, DeliverChannel.class, DeliverStatus.class, DeliverTtype.class, CustomLinkDeliverReqMethod.class, CustomLinkDeliverLinkAgingType.class, CustomLinkDeliverParamOnlyAgingType.class, DeliverExtparamsSource.class, DeliverExtparamsStatus.class, DeliverExtparamsExtparamType.class, DeliverExtparamsDeliverExtparamType.class, AgainDeliverRecordSendStatus.class, AgainDeliverRecordSurveyStatus.class, AgainDeliverRecordDeliverType.class, AgainDeliverTaskTtype.class, AgainDeliverTaskConditionRelation.class, WechatMPRecordStatus.class, WechatMPRecordRspdStatus.class})
public interface DeliverGrpcConvertMapper {

    default SmtpDto toSmtpDto(Smtp smtp) {
        if (Objects.isNull(smtp)) {
            return null;
        }
        SmtpDto smtpDto = new SmtpDto();
        smtpDto.setId(smtp.getId());
        smtpDto.setOrgId(smtp.getOrgId());
        smtpDto.setAccount(smtp.getAccount());
        smtpDto.setPassword(smtp.getPassword());
        smtpDto.setHost(smtp.getHost());
        smtpDto.setPort(String.valueOf(smtp.getPort()));
        smtpDto.setSSL(smtp.getSsl());
        smtpDto.setDescription(smtp.getDescription());
        smtpDto.setMailFrom(smtp.getSenderName());
        smtpDto.setValidateStatus(smtp.getStatusValue());
        smtpDto.setDeliver(smtp.getDeliver());
        smtpDto.setNotice(smtp.getNotice());
        smtpDto.setSystem(smtp.getSystem());
        return smtpDto;
    }

    default RespondentDto infoToRespondentDto(Respondent respondent) {
        RespondentDto respondentDto = new RespondentDto();

        respondentDto.setId(respondent.getId());
        respondentDto.setProjectId(respondent.getProjectId());
        respondentDto.setDataStatus(respondent.getDataStatus().getNumber());
        respondentDto.setStatus(respondent.getStatus().getNumber());
        respondentDto.setPreDiscardStatus(respondent.getPreDiscardStatus());
        respondentDto.setSeq(respondent.getSeq());
        respondentDto.setParsed(respondent.getParsed());
        respondentDto.setVersion(respondent.getVersion());
        respondentDto.setSource(respondent.getSource().getNumber());
        respondentDto.setGroupId(respondent.getGroupId());
        respondentDto.setGroupCode(respondent.getGroupCode());
        respondentDto.setPersonId(respondent.getPersonId());
        respondentDto.setUserAgent(respondent.getUserAgent());
        respondentDto.setClientType(respondent.getClientType().getNumber());
        respondentDto.setIp(respondent.getIp());
        respondentDto.setBeginTime(Instant.ofEpochMilli(Timestamps.toMillis(respondent.getBeginTime())).atZone(DateUtil.UTC_ZONE).toLocalDateTime());
        respondentDto.setFinishTime(Instant.ofEpochMilli(Timestamps.toMillis(respondent.getFinishTime())).atZone(DateUtil.UTC_ZONE).toLocalDateTime());
        respondentDto.setRspdDataId(respondent.getRspdDataId());
        respondentDto.setRspdWeixinId(respondent.getRspdWeixinId());
        respondentDto.setRspdLocationId(respondent.getRspdLocationId());
        respondentDto.setRspdExtraId(respondent.getRspdExtraId());
        respondentDto.setRspdAttachmentId(respondent.getRspdAttachmentId());
        respondentDto.setExtSeq(respondent.getExtSeq());
        respondentDto.setSubmitSeq(respondent.getSubmitSeq());
        respondentDto.setIpCountry(respondent.getIpCountry());
        respondentDto.setIpProvince(respondent.getIpProvince());
        respondentDto.setIpCity(respondent.getIpCity());
        respondentDto.setRemark(respondent.getRemark());
        respondentDto.setMemberId(respondent.getMemberId());
        respondentDto.setCurrentLang(respondent.getCurrentLang());
        respondentDto.setDeliverId(respondent.getDeliverId());
        respondentDto.setDeliverGroupId(respondent.getDeliverGroupId());
        respondentDto.setAgainDeliverRecordId(respondent.getAgainDeliverRecordId());
        respondentDto.setQuotaCheckitemList(respondent.getQuotaCheckitemList());
        respondentDto.setCreatedDt(DateUtil.getLocalDateTime(respondent.getCreatedDt()));
        respondentDto.setExcelParsed(respondent.getExcelParsed());
        respondentDto.setSurveyGids(respondent.getSurveyGidsList());
        respondentDto.setWxInfo(respondent.getWxInfo());
        respondentDto.setDedCode(respondent.getDedCode());
        respondentDto.setIdySid(respondent.getIdySid());
        respondentDto.setScore(respondent.getScore());

        return respondentDto;
    }

    default RespondentDto responseToRespondentDto(GetRespondentBySeqResponse response) {

        RespondentDto respondentDto = infoToRespondentDto(response.getRespondent());
        respondentDto.setAnswers(response.getAnswer());
        RespondentExtraDto respondentExtraDTO = new RespondentExtraDto();
        RespondentExtra extra = response.getExtra();
        respondentExtraDTO.setId(extra.getId());
        respondentExtraDTO.setProjectId(extra.getProjectId());
        respondentExtraDTO.setRid(extra.getRid());
        respondentExtraDTO.setSimilarStatus(extra.getSimilarStatus().getNumber());
        respondentExtraDTO.setBrandInfo(extra.getBrandInfo());
        respondentExtraDTO.setModelInfo(extra.getModelInfo());

        respondentDto.setExtra(respondentExtraDTO);

        MemberNodeDto memberNodeDto = new MemberNodeDto();
        MemberNode memberNode = response.getMember();
        memberNodeDto.setName(memberNode.getName());
        memberNodeDto.setGender(memberNode.getGender());
        memberNodeDto.setPhone(memberNode.getPhone());
        memberNodeDto.setEmail(memberNode.getEmail());
        respondentDto.setMember(memberNodeDto);

        respondentDto.setMemberTags(response.getMemberTagsList());
        respondentDto.setExtParams(response.getExtParam());
        return respondentDto;
    }

    @Mapping(target = "status", expression = "java(organization.getStatusValue())")
    @Mapping(target = "source", expression = "java(organization.getSourceValue())")
    @Mapping(target = "footer", expression = "java(organization.getFooterValue())")
    @Mapping(target = "deliverCheck", source = "deliverAudit")
    @Mapping(target = "isDeliverCheck", source = "deliverAudit")
    @Mapping(target = "complete", source = "complete")
    @Mapping(target = "checkSmtpId", source = "auditSmtpId")
    OrgInfoDto toOrgInfoDto(com.bestcem.bp.xm.user.grpc.v1.services.Organization organization);

    WechatDeliverInfo toGrpcWechatDeliverInfo(DeliverWechatDeliver dliver);

    List<WechatOfficialInfoDto> toWechatOfficialInfoDtoList(List<WechatOfficialAccountInfo> list);

    List<WechatMsgTemplateDto> toWechatMsgTemplateDtoList(List<MsgTemplate> list);

    @Mapping(target = "status", expression = "java(user.getStatus().getNumber())")
    @Mapping(target = "edition", expression = "java(user.getEdition().getNumber())")
    @Mapping(target = "userIndex", expression = "java(user.getIndex().getNumber())")
    UserDto toUserDto(User user);

    @Mapping(target = "fieldId", source = "id")
    @Mapping(target = "type", expression = "java(item.getType().getNumber())")
    MemberDto.ExtendFieldValueDto toExtendFieldValueDto(ExtendFieldValue item);


    MemberDto.LabelValueDto toLabelValueDto(LabelValue item);

    @Mapping(target = "mobile", source = "phone")
    @Mapping(target = "phone", source = "phone")
    @Mapping(target = "store", source = "storeId")
    @Mapping(target = "source", expression = "java(member.getSource().getNumber())")
    @Mapping(target = "status", expression = "java(member.getStatus().getNumber())")
    @Mapping(target = "memberType", expression = "java(member.getMemberType().getNumber())")
    @Mapping(target = "employeeStatus", expression = "java(member.getEmployeeStatus().getNumber())")
    @Mapping(target = "memberDeliveryStatus", expression = "java(member.getMemberDeliveryStatus().getNumber())")
    @Mapping(target = "subscribeStatus", expression = "java(member.getSubscribeStatus().getNumber())")
    MemberDto toMemberDto(Member member);

    @Mapping(target = "memberType", expression = "java(item.getMemberType().getNumber())")
    @Mapping(target = "type", expression = "java(item.getType().getNumber())")
    MemberProjectDto.MemberProjectExtendFieldValueDto toMemberProjectExtendFieldValueDto(ExtendFieldValue item);

    MemberProjectDto toMemberProjectDto(com.bestcem.xm.member.grpc.v1.common.Project project);

    @Mapping(target = "status", expression = "java(item.getStatus().getNumber())")
    @Mapping(target = "dataStatus", expression = "java(item.getDataStatus().getNumber())")
    @Mapping(target = "source", expression = "java(item.getSource().getNumber())")
    @Mapping(target = "clientType", expression = "java(item.getClientType().getNumber())")
    SurveyRespondentDto toSurveyRespondentDto(com.bestcem.xm.survey.grpc.v1.services.Respondent item);

    List<SurveyRespondentDto> toSurveyRespondentDtoList(List<com.bestcem.xm.survey.grpc.v1.services.Respondent> list);

    @Mapping(target = "sendStatus", expression = "java(AgainDeliverRecordSendStatus.forNumber(deliverRecord.getSendStatus()))")
    @Mapping(target = "surveyStatus", expression = "java(AgainDeliverRecordSurveyStatus.forNumber(deliverRecord.getSurveyStatus()))")
    @Mapping(target = "deliverType", expression = "java(AgainDeliverRecordDeliverType.forNumber(deliverRecord.getDeliverType()))")
    AgainDeliverRecord toGrpcAgainDeliverRecord(DeliverAgainDeliverRecord deliverRecord);

    @Mapping(target = "ttype", expression = "java(AgainDeliverTaskTtype.forNumber(deliverAgainDeliverTask.getTtype()))")
    @Mapping(target = "conditionRelation", expression = "java(AgainDeliverTaskConditionRelation.forNumber(deliverAgainDeliverTask.getConditionRelation()))")
    AgainDeliverTask toGrpcAgainDeliverTask(DeliverAgainDeliverTask deliverAgainDeliverTask);

    @Named("deliverExtparamType")
    default Integer deliverExtparamType(DeliverExtparamsDeliverExtparamType item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Named("extparamsSource")
    default Integer extparamsSource(DeliverExtparamsSource item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Named("extparamsExtparamType")
    default Integer extparamsExtparamType(DeliverExtparamsExtparamType item) {
        if (item == null) {
            return null;
        }
        return item.getNumber();
    }

    @Mapping(target = "surveyId", source = "surveyId")
    @Mapping(target = "requireFlag", source = "require")
    @Mapping(target = "aesEncryptStr", source = "aesEncrypt")
    @Mapping(target = "deliverExtparamType", source = "deliverExtparamType", qualifiedByName = "deliverExtparamType")
    @Mapping(target = "source", source = "source", qualifiedByName = "extparamsSource")
    @Mapping(target = "extparamType", source = "extparamType", qualifiedByName = "extparamsExtparamType")
    @Mapping(target = "status", expression = "java(item.getStatusValue())")
    DeliverDeliverExtParams toDeliverDeliverExtParams(com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams item);

    DeliverExtDeliverMapping toDeliverExtDeliverMapping(com.bestcem.xm.deliver.grpc.v1.services.ExtDeliverMapping item);

    @Named("deliverExtparamType")
    default DeliverExtparamsDeliverExtparamType deliverExtparamType(Integer deliverExtparamType) {
        if (deliverExtparamType == null) {
            return null;
        }
        return DeliverExtparamsDeliverExtparamType.forNumber(deliverExtparamType);
    }

    @Named("extparamsSource")
    default DeliverExtparamsSource extparamsSource(Integer extparamsSource) {
        if (extparamsSource == null) {
            return null;
        }
        return DeliverExtparamsSource.forNumber(extparamsSource);
    }

    @Named("extparamsExtparamType")
    default DeliverExtparamsExtparamType extparamsExtparamType(Integer extparamsSource) {
        if (extparamsSource == null) {
            return null;
        }
        return DeliverExtparamsExtparamType.forNumber(extparamsSource);
    }

    @Mapping(target = "require", source = "requireFlag")
    @Mapping(target = "aesEncrypt", source = "aesEncryptStr")
    @Mapping(target = "deliverExtparamType", source = "deliverExtparamType", qualifiedByName = "deliverExtparamType")
    @Mapping(target = "source", source = "source", qualifiedByName = "extparamsSource")
    @Mapping(target = "extparamType", source = "extparamType", qualifiedByName = "extparamsExtparamType")
    @Mapping(target = "status", expression = "java(DeliverExtparamsStatus.forNumber(item.getStatus()))")
    com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams toGrpcDeliverExtparams(DeliverDeliverExtParams item);

    List<com.bestcem.xm.deliver.grpc.v1.services.DeliverExtparams> toGrpcDeliverExtparamsList(List<DeliverDeliverExtParams> items);

    List<com.bestcem.xm.deliver.grpc.v1.services.ExtDeliverMapping> toGrpcDeliverExtDeliverMappingList(List<DeliverExtDeliverMapping> items);

    default DeliverCustomLinkDeliver toDeliverCustomLinkDeliver(CustomLinkDeliver item) {
        if (item == null) {
            return null;
        }

        //deliver_id survey_id param_only_aging_type=1 3个参数
        DeliverCustomLinkDeliver deliverCustomLinkDeliver = new DeliverCustomLinkDeliver();

        deliverCustomLinkDeliver.setSurveyId(item.getSurveyId());
        deliverCustomLinkDeliver.setDeliverId(item.getDeliverId());
        //grpc 的枚举 真特么烦人
        if (item.getParamOnlyAgingTypeValue() > 0) {
            deliverCustomLinkDeliver.setParamOnlyAgingType(item.getParamOnlyAgingTypeValue());
        }

        return deliverCustomLinkDeliver;
    }

    @Mapping(target = "reqMethod", expression = "java(CustomLinkDeliverReqMethod.forNumber(item.getReqMethod()))")
    @Mapping(target = "linkAgingType", expression = "java(CustomLinkDeliverLinkAgingType.forNumber(item.getLinkAgingType()))")
    @Mapping(target = "paramOnlyAgingType", expression = "java(CustomLinkDeliverParamOnlyAgingType.forNumber(item.getParamOnlyAgingType()))")
    com.bestcem.xm.deliver.grpc.v1.services.CustomLinkDeliver toGrpcDeliverExtparams(DeliverCustomLinkDeliver item);

    List<com.bestcem.xm.deliver.grpc.v1.services.CustomLinkDeliver> toGrpcCustomLinkDeliverList(List<DeliverCustomLinkDeliver> items);


    @Named("delayFailedttype")
    default DeliverDelayFailedTtype delayFailedttype(Integer delayFailedTtype) {
        if (delayFailedTtype == null) {
            return null;
        }
        return DeliverDelayFailedTtype.forNumber(delayFailedTtype);
    }

    @Named("awardUseType")
    default DeliverAwardUseType awardUseType(Integer type) {
        if (type == null) {
            return null;
        }
        return DeliverAwardUseType.forNumber(type);
    }

    @Mapping(target = "ttype", expression = "java(DeliverTtype.forNumber(item.getTtype()))")
    @Mapping(target = "status", expression = "java(DeliverStatus.forNumber(item.getStatus()))")
    @Mapping(target = "channel", expression = "java(DeliverChannel.forNumber(item.getChannel()))")
    @Mapping(target = "scenes", expression = "java(DeliverScenes.forNumber(item.getScenes()))")
    //@Mapping(target = "delayFailedttype", expression = "java(DeliverDelayFailedTtype.forNumber(item.getDelayFailedTtype()))")
    @Mapping(target = "delayFailedttype", source = "delayFailedTtype", qualifiedByName = "delayFailedttype")
    //@Mapping(target = "awardUseType", expression = "java(DeliverAwardUseType.forNumber(item.getAwardUseType()))")
    @Mapping(target = "awardUseType", source = "awardUseType", qualifiedByName = "awardUseType")
    @Mapping(target = "delayFailedtime", source = "delayFailedTime")
    com.bestcem.xm.deliver.grpc.v1.services.Deliver toGrpcDeliver(DeliverDeliver item);


    @Mapping(target = "status", expression = "java(WechatMPRecordStatus.forNumber(item.getStatus()))")
    @Mapping(target = "rspdStatus", expression = "java(WechatMPRecordRspdStatus.forNumber(item.getRspdStatus()))")
    WechatMPRecord toGrpcWechatMPRecord(DeliverWechatmpRecord item);

    SmsSendingResultDto sendSingleResultGrpc2Dto(SendSingleResult result);

    com.bestcem.xm.deliver.grpc.v1.services.SurveyExtParamsVersion toGrpcDeliverSurveyExtParamsVersion(DeliverSurveyExtParamsVersion item);

    List<com.bestcem.xm.deliver.grpc.v1.services.SurveyExtParamsVersion> toGrpcDeliverSurveyExtParamsVersionList(List<DeliverSurveyExtParamsVersion> items);

    com.bestcem.xm.deliver.grpc.v1.services.SurveyDeliver toGrpcDeliverSurveyDeliver(DeliverSurveyDeliver item);

    List<com.bestcem.xm.deliver.grpc.v1.services.SurveyDeliver> toGrpcDeliverSurveyDeliverList(List<DeliverSurveyDeliver> list);
}
